/*
 * Copyright 2008 Nordic Consulting & Development Company
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package pl.ncdc.web.login.client.ui;

import pl.ncdc.web.login.client.User;

import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;

/**
 * Factory creating widgets which present user data.
 * <p>
 * Created on Apr 14, 2008
 *
 * @author hshsce
 * @version $Id: DefaultUserWidgetFactory.java 21 2008-04-17 11:15:19Z hshsce $
 */
public class DefaultUserWidgetFactory implements UserWidgetFactory {

	public static final String WIDGET_ID = "ncdc-web-login-UserWidget";

	/**
	 * Widget CSS styles.
	 */
	public interface Style {

		/**
		 * User image: <code>.Image</code>
		 */
		String IMAGE = "Image";

		/**
		 * User details: <code>.Details</code>
		 */
		String DETAILS = "Details";

		/**
		 * User ID: <code>.Uid</code>. 
		 */
		String UID = "Uid";

		/**
		 * Full Name: <code>.FullName</code>
		 */
		String FULL_NAME = "FullName";
	}

	private final String m_imagesUrl;

	private final String m_imageExtension;

	public DefaultUserWidgetFactory(final String imagesUrl) {
		this(imagesUrl, null);
	}

	/**
	 * If imageExtension does not start with '.' it will be automatically prepended.
	 *
	 * @param imagesUrl
	 * @param imageExtenstion
	 */
	public DefaultUserWidgetFactory(final String imagesUrl, final String imageExtenstion) {
		if (imagesUrl == null) {
			throw new IllegalArgumentException("imagesUrl cannot be null");
		}
		m_imagesUrl = imagesUrl;
		if ((imageExtenstion == null) || (imageExtenstion.trim().length() == 0)) {
			m_imageExtension = "";
		} else if (imageExtenstion.startsWith(".")) {
			m_imageExtension = imageExtenstion;
		} else {
			m_imageExtension = "." + imageExtenstion;
		}
	}

	public Widget newWidget(final User user) {
		final Image img = new Image(m_imagesUrl + "/" + user.getUid() + m_imageExtension);
		final Grid grid = new Grid(1, 2);
		grid.setCellPadding(0);
		grid.setCellSpacing(0);
		final CellFormatter f = grid.getCellFormatter();
		grid.setWidget(0, 0, img);
		f.setStyleName(0, 0, Style.IMAGE);
		grid.setWidget(0, 1, newDetails(user));
		f.setStylePrimaryName(0, 1, Style.DETAILS);
		grid.setStyleName(WIDGET_ID);
		return grid;
	}

	private Widget newDetails(final User user) {
		final Grid grid = new Grid(2, 1);
		grid.setCellPadding(0);
		grid.setCellSpacing(0);
		final CellFormatter f = grid.getCellFormatter();
		grid.setText(0, 0, user.getUid());
		f.setStylePrimaryName(0, 0, Style.UID);
		grid.setText(1, 0, user.getFullName());
		f.setStylePrimaryName(1, 0, Style.FULL_NAME);
		return grid;
	}

}
